package com.chenjun.web.controller.sys;
import java.lang.reflect.InvocationTargetException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.core.dao.generator.entity.SysLog;
import com.chenjun.core.service.sys.SysLogService;
import com.chenjun.core.vo.sys.SysLogDto;
import com.chenjun.web.controller.BaseController;

/**
 * 
 * @Description: 日志控制器 
 * @author yanjinyin
 * @date 2017年7月23日 下午4:13:02
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping("sysLog")
public class SysLogController extends BaseController {

	@Autowired
	private SysLogService sysLogService;

	/**
	 * 页面展示
	 * @return
	 * ModelAndView
	 */
	@RequestMapping("/list.html")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("sysLog/list");
		//补充业务逻辑
		return mv;
	}
	
	/**
	 * 分页搜索
	 * @param datagrid
	 * @return
	 * ResultDto
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value = "/search")
	@ResponseBody
	public ResultDto list(DataGridModel datagrid) {
		ResultDto rtDto = new ResultDto();
		Pagination data = sysLogService.doPagination(datagrid);
		rtDto.setData(data);
		return rtDto;
	}

	/**
	 * 编辑页面元素
	 * TODO
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public ResultDto edit(SysLogDto dto)  throws IllegalAccessException, InvocationTargetException{
		ResultDto rtDto = new ResultDto();
		int i = sysLogService.createOrUpdate(SysLogDto.getModelByDto(dto));
		if(i<=0){
			rtDto.resultError();
			rtDto.setMessage("编辑系统异常");
		}
		return rtDto;
	}

	/**
	 * 删除指定的记录
	 * TODO
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public ResultDto delete(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		SysLog sysLog = sysLogService.get(id);
		int result = 0;
		if (sysLog != null) {
			result = sysLogService.delete(id);
		}
		if (result <= 0) {
			rtDto.resultError();
			rtDto.setMessage("要删除的记录不存在!");
		}
		return rtDto;
	}

	/**
	 * 根据参数获取信息
	 * TODO
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/get/{id}")
	@ResponseBody
	public ResultDto get(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		rtDto.setData(sysLogService.get(id));
		return rtDto;
	}

}
